import *as types from '../mutation-types'
const state ={
   added:[]
}
const getters={
 shopCart:state=>state.added
}

const mutations ={
  [types.ADD_TO_CART](state,{obj}){
    let record=state.added.find(item=>item.name===obj.name)
    if(!record){
      state.added.push({
        name:obj.name,
        count:obj.count,
        price:obj.price
        })
      } else {
      record.count++
    }
    console.log(state.added)
  },
  [types.DEV_TO_CART](state,{obj}){
    let record=state.added.find(item=>item.name===obj.name)
    if(record){
      record.count--
      if(record.count<=0){
        state.added.splice(state.added.indexOf(record),1)
      }
      console.log(state.added)
    }
  }
}

const actions={
  cleanCart(){
    state.added=[]
  }
}

export default {
  state,
  mutations,
  getters,
  actions
}
